<%@ page language="java" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>Java后端WebSocket的Tomcat实现</title>

    <style>
        .box1{
            width: 802px;
            margin: auto;
            height: auto;
            background-color: #ccddde;
        }
        .box2{
            width: 802px;
            height: 100px;
            margin: auto;
            background-color: #ccddde;
        }
        .box3{
            width: 802px;
            margin: auto;
            text-align: center;
            background-color: #ccddde;
        }
        .box4{
            color: #c71b13;
            font-size: 30px;
            background-color: #797dae;
        }

    </style>
    <script src="/js/jquery-3.2.1.js"></script>
</head >
<body>

<div class="box3">
<div class="box4">聊天窗口</div>
</div>
<div id="message" class="box1"></div>

<div class="box2">
     <textarea placeholder="回复" id="send"  cols="140" rows="5" style="resize:none;"></textarea><br>
    接收人:<input type="text" id="receiver" value="">
    <button onclick="send()">发送消息</button>

</div>

<hr/>
<button onclick="closeWebSocket()">关闭WebSocket连接</button>
<hr/>

</body>

<script type="text/javascript">
    var websocket = null;

    var username= RndNum(5);
    //判断当前浏览器是否支持WebSocket
    if ('WebSocket' in window) {
        websocket = new WebSocket("ws://172.16.16.219:8080/websocket/"+username);
    }
    else {
        alert('当前浏览器 Not support websocket')
    }

    //连接发生错误的回调方法
    websocket.onerror = function () {
        setMessageInnerHTML("WebSocket连接发生错误");
    };

    //连接成功建立的回调方法
    websocket.onopen = function () {
        setMessageInnerHTML("WebSocket连接成功");
    }

    //接收到消息的回调方法
    websocket.onmessage = function (event) {
        setMessageInnerHTML(event.data);
    }

    //连接关闭的回调方法
    websocket.onclose = function () {
        setMessageInnerHTML("WebSocket连接关闭");
    }

    //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
    window.onbeforeunload = function () {
        closeWebSocket();
    }

    //将消息显示在网页上
    function setMessageInnerHTML(innerHTML) {
        document.getElementById('message').innerHTML += innerHTML + '<br/>'+"<br />";
    }

    //关闭WebSocket连接
    function closeWebSocket() {
        websocket.close();
    }

    //发送消息
    function send() {
        var message = document.getElementById('send').value;
        if (message!=""){
            document.getElementById("send").value="";
            var receiver =document.getElementById("receiver").value;
            message = message+":"+receiver;
            websocket.send(message);
        }else {
            setMessageInnerHTML("<span style='color:red'>请输入内容</span>")
        }

    }
    function getname(username) {
       document.getElementById("receiver").value=username

        document.getElementById("send").setAttribute("placeholder","回复"+username)
    }

    function RndNum(n){
        var rnd="1";
        for(var i=0;i<n-1;i++)
            rnd+=Math.floor(Math.random()*10);
        return rnd;
    }
    $("#receiver").blur(function () {
        var str = document.getElementById("receiver").value;
        if (str != "") {

            document.getElementById("send").setAttribute("placeholder", "发送给" + str)
        }else {
            document.getElementById("send").setAttribute("placeholder", "请输入要发送的内容")
        }

    });
</script>
</html>
